package Search;

import Data.PrivateInformationData;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.*;

public class SearchInformationById {
    private String id;
    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public SearchInformationById(String id) {
        this.id = id;
    }

    public PrivateInformationData getPIDdata() throws ClassNotFoundException, SQLException, IOException {
        PrivateInformationData data=new PrivateInformationData();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql = "select *FROM userinformation where id=?;";

        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,id);
        ResultSet resultSet=operation.executeQuery();
        if(resultSet.next())
        {
            //读取头像
            String path=resultSet.getString("avatar");
            File file=new File(path);
            byte[] t=null;
            if(file!=null)
            {
                byte[] buffer=new byte[1024];
                t=new byte[(int) file.length()];
                InputStream is= Files.newInputStream(file.toPath());
                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                int len;
                while((len=is.read(buffer))!=-1)
                {
                    bos.write(buffer,0,len);
                }
                t=bos.toByteArray();
            }

            data.setAvatar(t);

            data.setEmailNumber(resultSet.getString("emailNumber"));
            data.setSex(resultSet.getString("sex"));
            data.setSaying(resultSet.getString("saying"));
            data.setUserid(id);
            data.setUserNickName(resultSet.getString("userNickName"));
            data.setBirth(resultSet.getDate("birth"));
        }
        return data;
    }
}
